import { Toast } from 'antd-mobile'
import React, { useEffect, useState } from 'react'

export default function useHandleScroll(domTagName: string, total: number) {
    const [filterParams,setFilterParams]=useState({page:1,size:5})
    useEffect(() => {
        const dom = document.getElementById(domTagName)
        dom?.addEventListener("scroll", (e: any) => {
            const { scrollTop, clientHeight, scrollHeight } = e.target;
            // console.log(scrollTop, clientHeight, scrollHeight,total)
            if(scrollTop+clientHeight==scrollHeight){
                if(filterParams.page*filterParams.size>=total && total!=0){
                    Toast.show({
                        content:'已滑到底~'
                    })
                    return
                }
                setFilterParams({...filterParams,page:(filterParams.page+=1)})
            }
        })
        return () => {
            dom?.removeEventListener("scroll", () => { })
        }
    }, [total,filterParams])
    return {
        filterParams,
        setFilterParams
    }
}
